package com.muyu.cloud.commodity.controller;

import com.muyu.cloud.commodity.domain.req.ProductReq;
import com.muyu.cloud.commodity.domain.model.ProductUpdateModel;
import com.muyu.cloud.commodity.domain.req.ProductUpdateReq;
import com.muyu.cloud.commodity.domain.req.ProductAddNewReq;
import com.muyu.cloud.commodity.domain.resp.ProductDetailResp;
import com.muyu.cloud.commodity.domain.resp.ProductVo;
import com.muyu.cloud.commodity.service.ProductService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.cloud.commodity.domain.req.ProductCommentListReq;
import com.muyu.cloud.commodity.domain.resp.ProductCommentListResp;
import com.muyu.cloud.commodity.domain.req.ProductCommentAuditReq;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author <a href="mailto:602048165@qq.com">60204</a>
 * @version 1.0
 * @description:
 * @date 2025/9/14 16:51
 */
@RestController
@RequestMapping("/product")
@Tag(name = "ProductController",description = "商品管理")

public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;
    /**
     * 获取商品列表（管理端）
     * @param productReq
     * @return
     */
    @Operation(summary = "获取商品分页列表（管理端）")
    @RequiresPermissions("product:list")
    @PostMapping("/list")
    public Result<DataPageResp<ProductVo>>list(@RequestBody ProductReq productReq){
      DataPageResp<ProductVo>resultList=  productService.pageQuery(productReq);
    return Result.success(resultList);
    }

    /**
     * 获取商品列表（公开接口，无需认证）
     * @param productReq
     * @return
     */
    @Operation(summary = "获取商品分页列表（公开接口）")
    @PostMapping("/public/list")
    public Result<DataPageResp<ProductVo>> publicList(@RequestBody ProductReq productReq){
        // 只返回上架的商品
        productReq.setStatus(1); // 1表示上架状态
        DataPageResp<ProductVo> resultList = productService.pageQuery(productReq);
        return Result.success(resultList);
    }

    /**
     * 获取商品详情（管理端）
     * @param id
     * @return
     */
    @Operation(summary = "获取商品详情（管理端）")
    @RequiresPermissions("product:query")
    @GetMapping("/{id}")
    public Result<ProductDetailResp>getInfo(@PathVariable("id")Long id){
        return Result.success(productService.getDetilById(id));
    }

    /**
     * 获取商品详情（公开接口，无需认证）
     * @param id
     * @return
     */
    @Operation(summary = "获取商品详情（公开接口）")
    @GetMapping("/public/{id}")
    public Result<ProductDetailResp> getPublicInfo(@PathVariable("id") Long id){
        return Result.success(productService.getDetilById(id));
    }

    /**
     * 商品评论列表
     * @param req
     * @return
     */
    @Operation(summary = "获取商品评论分页列表")
    @RequiresPermissions("product:comment:list")
    @PostMapping("/comment/list")
    public Result<DataPageResp<ProductCommentListResp>> commentList(@RequestBody ProductCommentListReq req) {
        return Result.success(productService.pageComments(req));
    }

    /**
     * 商品评价通过
     * @param req
     * @return
     */
    @Operation(summary = "评论通过")
    @RequiresPermissions("product:comment:approve")
    @PostMapping("/comment/approve")
    public Result<Void> approve(@RequestBody ProductCommentAuditReq req) {
        productService.approveComments(req.getIds());
        return Result.success();
    }

    /**
     * 商品评价驳回
     * @param req
     * @return
     */
    @Operation(summary = "评论驳回")
    @RequiresPermissions("product:comment:reject")
    @PostMapping("/comment/reject")
    public Result<Void> reject(@RequestBody ProductCommentAuditReq req) {
        productService.rejectComments(req.getIds());
        return Result.success();
    }

    /**
     * 商品修改
     * @param req
     * @return
     */
    @Operation(summary = "修改商品")
    @RequiresPermissions("product:edit")
    @PutMapping
    public Result<String> edit(@Validated @RequestBody ProductUpdateReq req){
        ProductUpdateModel productUpdateModel = ProductUpdateModel.buildModel(req);
        productService.upddateProduct(productUpdateModel);
        return Result.success();
    }

    /**
     * 商品上架
     */
    @Operation(summary = "商品上架")
    @RequiresPermissions("product:up")
    @PutMapping("/up/{id}")
    public Result<Void> up(@PathVariable("id") Long id){
        productService.upProduct(id);
        return Result.success();
    }

    /**
     * 商品下架
     */
    @Operation(summary = "商品下架")
    @RequiresPermissions("product:down")
    @PutMapping("/down/{id}")
    public Result<Void> down(@PathVariable("id") Long id){
        productService.downProduct(id);
        return Result.success();
    }

    /**
     * 商品添加
     * 包含规格组、规格值和SKU信息
     * @param req
     * @return
     */
    @Operation(summary ="新增商品")
    @RequiresPermissions("product:add")
    @PostMapping
    public Result<Void> addProduct(@Validated @RequestBody ProductAddNewReq req){
        productService.addProduct(req);
        return Result.success();
    }






}
